﻿using WebApp.BLL.Base;
using WebApp.Common;
using WebApp.DAL;
using WebApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebApp.BLL.Manage
{
    public class S_roleService : BaseService<S_role>
    {

        #region (DAL)Repository
        
        internal S_role_menuRepository rolemenuRespository = new S_role_menuRepository();
        internal S_menuRepository menuRepository = new S_menuRepository();
        //internal S_customer_roleRepository custroleRepository = new S_customer_roleRepository();

        #endregion

        #region 构造

        public S_roleService() : base(new S_roleRepository()) { }

        #endregion

        #region PageList

        public List<S_role> PageList(int pageindex, int pagesize, string searchkey, string sort, string order, out int rowscount)
        {

            var T = this.CurrentRepository.Entities.Where(p => p.Isdeleted == "0");
            //if (string.IsNullOrEmpty(sort)) sort = "Roleid";

            //根据属性名实现动态排序
            if (string.IsNullOrEmpty(sort)) sort = "Id";
            T = base.SortList(T, sort, order == "asc" ? true : false);

            //查询条件
            if (!string.IsNullOrEmpty(searchkey))
            {
                T = T.Where(p => p.Rolename.Contains(searchkey) || p.Description.Contains(searchkey));
            }

            //返回记录数
            rowscount = T.Count();
            //返回结果
            return this.CurrentRepository.PageList(T, pageindex, pagesize).ToList();
        }

        #endregion

        #region FindByRoleid

        public S_role FindByRoleid(string roleid)
        {
            return this.CurrentRepository.Entities
                .FirstOrDefault(p => p.Isdeleted == "0" && p.Roleid == roleid);
        }

        #endregion

        #region Save

        public bool Save(S_role item)
        {
            if (item.Id == 0)
            {
                item.Isdeleted = "0";
                this.CurrentRepository.Add(item);
            }
            else
            {
                item.Isdeleted = "0";
                this.CurrentRepository.Update(item);
            }

            return true;
        }

        #endregion

        #region DeleteById

        public bool DeleteById(int id)
        {
            var role = this.CurrentRepository.Find(id);

            if (role != null)
            {
                var rolemenus = this.rolemenuRespository.Entities.Where(p => p.Roleid == role.Roleid).ToList();

                foreach (var item in rolemenus)
                {
                    this.rolemenuRespository.Delete(item, false);
                }

                this.CurrentRepository.Delete(role, false);

                this.CurrentRepository.Save();
            }

            return true;
        }

        #endregion

        #region DeleteByIds

        public bool DeleteByIds(string ids)
        {
            foreach (string id in ids.Split(','))
            {
                if (string.IsNullOrEmpty(id)) continue;

                this.DeleteById(Utils.NvInt(id));
            }

            return true;
        }

        #endregion

        #region Exist

        public bool Exist(int id, string roleid)
        {
            var T = this.CurrentRepository.Entities;
            if (id == 0)
            {
                return T.FirstOrDefault(p => p.Roleid == roleid) != null;
            }
            else
            {
                return T.FirstOrDefault(p => p.Id != id && p.Roleid == roleid) != null;
            }
        }

        //public bool ExistStudy(string roleid, int studyid, string custcode)
        //{
        //    var T = this.CurrentRepository.Entities.Where(p => p.Custcode == custcode)
        //        .Where(p => p.Study_id == studyid)
        //        .Where(p => p.Roleid == roleid)
        //        .Where(p => p.Isdeleted == "0").ToList();
        //    if (T.Count > 0)
        //    {
        //        return false;
        //    }
        //    else
        //    {
        //        return true;
        //    }

        //}

        #endregion

        #region 生成一个随机角色编号

        public string GetNewRoleid(string custcode)
        {
            var roleid = custcode + "." + Utils.GetRandomStr(4);

            while (true)
            {
                var check = this.CurrentRepository.Entities.FirstOrDefault(p => p.Roleid == roleid);

                if (check == null)
                {
                    break;
                }

                roleid = custcode + "." + Utils.GetRandomStr(4);
            }

            return roleid;
        }

        #endregion
        
        #region GetRoleNamesByUserid

        public string GetRoleNamesByUserid(int userid)
        {
            var userroleDAL = new S_user_roleRepository();

            List<string> userroles =  userroleDAL.Entities.Where(p => p.User_id.Value == userid).Select(p => p.Roleid).ToList();
            List<S_role> roles = this.CurrentRepository.Entities.Where(p => userroles.Contains(p.Roleid)).ToList();

            string names = "";
            foreach (S_role role in roles)
            {
                names += role.Rolename + "，";
            }

            return names.TrimEnd('，');
        }

        #endregion
    }
}